package com.sinodata.bsm.cicp.finder.linux;

import java.io.BufferedReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;

import com.sinodata.bsm.cicp.finder.Finder;
import com.sinodata.bsm.cicp.util.CommandReader;

/**
 * <p>Title: LinuxOS</p>
 * <p>Description: Linux主机发现程序</p>
 * <p>Copyright: Copyright (c) 2005</p>
 * <p>Company: Beijing youlian chuangxin system integration CO.,LTD</p>
 *
 * @author zhaoch
 * @version 1.0
 * 
 */
public class LinuxOS extends Finder {
    private static Logger logger = Logger.getLogger(LinuxOS.class);
    private static String seperator = " ";
    private static final String CMDSTR = "echo `uname -n` `cat /proc/cpuinfo|grep processor|wc -l` `uname -s` `uname -m` `uname -r`";

    @SuppressWarnings("unchecked")
    @Override
    public List<String[]> find(String ip, Map params) {
        String[] osInfo = null;
        List<String[]> list = new ArrayList<String[]>();
        String strLine = null;
        BufferedReader reader = null;
        try {
            reader = CommandReader.getReader(ip, CMDSTR, 0, 0);
            if ((strLine = reader.readLine()) != null) {
                /*
                 * 每一个String[]表示一个资源数据，String[]数据定义如下：
                 * 第一个元素：资源的内部标示
                 * 第二个元素：资源名称
                 * 第三个元素：资源描述
                 * 第四-第N个参数：返回的属性值
                 */
                osInfo = new String[8];
                list.add(osInfo);
                osInfo[0] = "0";
                osInfo[1] = "LinuxOS";
                osInfo[2] = "LinuxOS";
                String[] ss = strLine.split("\\s+");
                //OS类型 uname -s
                //主机名 uname -n
                //OS版本 uname -r
                //主机型号 uname -m
                for (int i = 0; i < ss.length; i++) {
                    osInfo[3 + i] = ss[i];
                }
            }
        } catch (IOException e) {
            logger.error("failed to find LinuxOS", e);
        } finally {
            if (reader != null) {
                try {
                    reader.close();
                } catch (IOException e) {
                    logger.error("failed to close reader", e);
                }
            }
        }
        return list;
    }
}
